/*
* 增、删、改、列表共用方法
*/

import Service from '@/api/service';
export default {
  data () {
    return {
      datePicker: [],
      dateVipPicker: [],
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick (picker) {
            const end = new Date();
            const start = new Date();

            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一周',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一月',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      pickerVipOptions: {
        shortcuts: [{
          text: '半年会员',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 182.5);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '一年会员',
          onClick (picker) {
            const end = new Date();
            const start = new Date();
            end.setTime(start.getTime() + 3600 * 1000 * 24 * 365);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      listLoading: true,
      dialogVisible: false,
      dialogState: 'create',
      total: 0,
      page: 1,
      limit: 15,
      isDisabled: false,
      multipleSelection: [],
      APISERVER: '',
      dataList: [],
      params: {
      }, // 列表参数
      option: [{
        _id: '-1',
        name: '根选项'
      }],
      optionList: []
    };
  },
  computed: {
    moduleService (servicePath) {
      return new Service(servicePath || this.servicePath);
    }
  },
  methods: {
    pickerChange (value) {
      this.datePicker = value || [];
    },
    sortByDate (column, key, order) {
      console.log(column, key, order);
      // return obj2.createTime - obj1.createTime;
    },
    getOptionList (options) {
      this.moduleService.list(options || {}).then(res => {
        const _optionList = res.data.result.map(item => {
          return {
            _id: item._id,
            name: item.name
          };
        });
        this.optionList = this.option.concat(_optionList);
      });
    },
    handleUpdata (row) {
      this.dialogVisible = true;
      this.dialogState = 'update';
      if (row.isLock) {
        this.isDisabled = true;
      }
      this.initForm(row);
      this.updateTree();
    },
    handleOpen () {
      this.dialogState = 'create';
      this.dialogVisible = true;
      this.isDisabled = false;
      this.FormClear();
    },
    handleCancel () {
      this.dialogVisible = false;
      this.isDisabled = false;
      this.FormClear();
    },
    handleDelete (row) {
      this.$confirm('确认删除该记录吗？', '提示', { type: 'warning' })
        .then(() => {
          this.moduleService.delete({ id: row.id ? row.id : row._id }).then(response => {
            console.log(response)
            this.$message.success('删除成功');
            this.getList(this.page);
          });
        });
    },
    handleCurrentChange (val) {
      this.getList(val);
    },
    // 全选
    handleSelectionChange (val) {
      this.multipleSelection = val.map((item) => {
        return item.id;
      });
    },
    search () {
      this.getList();
    },
    arrDelete (type, ID) {
      this.$confirm('确认删除' + (type === '1' ? '本条' : '所选') + '记录吗？', '提示', { type: 'warning' })
        .then(() => {
          const ids = this.multipleSelection.map(id => `id=${id}`).join('&');

          this.$API[this.DELETEAPI](ID ? { id: ID } : ids).then(res => {
            if (res.data) {
              this.getList();
              this.$message.success('删除成功');
            } else {
              this.$message.error(res.message);
            }
          });
        });
    },
    getList (page) {
      this.listLoading = true;
      let option = {
        page: page || this.page,
        size: this.limit,
        ...this.params,
      };
      var searchInfo = this.searchInfo ? Object.keys(this.searchInfo) : []
      if (searchInfo.length !== 0) {
        option[this.searchField] = this[this.searchField];
      }
      if (this.datePicker && this.datePicker.length > 0) {
        option.beginTime = this.datePicker[0];
        option.endTime = this.datePicker[1];
      }
      this.dataList = [];
      this.$API[this.APISERVER](option).then((res) => {
        if (res.code === 200 && res.data && res.data.items.length > 0) {
          this.dataList = res.data.items;
        }
        this.total = res.data.totalSize;
        this.listLoading = false;
      }).catch(err => {
        console.log(err)
        this.total = 0;
        this.listLoading = false;
      });
    }
  }
};
